package com.qmx.launcher.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.open.androidtvwidget.bridge.RecyclerViewBridge;
import com.open.androidtvwidget.leanback.adapter.GeneralAdapter;
import com.open.androidtvwidget.leanback.recycle.LinearLayoutManagerTV;
import com.open.androidtvwidget.leanback.recycle.RecyclerViewTV;
import com.open.androidtvwidget.view.MainUpView;
import com.qmx.launcher.R;
import com.qmx.launcher.activity.base.BaseActivity;
import com.qmx.launcher.activity.general.AboutActivity;
import com.qmx.launcher.activity.general.GeneralTActivity;
import com.qmx.launcher.activity.general.NetTestActivity;
import com.qmx.launcher.activity.general.NetWorkActivity;
import com.qmx.launcher.activity.general.TVCameraActivity;
import com.qmx.launcher.activity.general.VideoSetActivity;
import com.qmx.launcher.activity.general.VoiceSetActivity;
import com.qmx.launcher.adapter.SettingRecyclerViewPresenter;
import com.qmx.launcher.view.CustomerOnOff;

/**
 *
 */
public class SettingActivity extends BaseActivity implements RecyclerViewTV.OnItemListener {

    private CustomerOnOff customerOnOff;
    private Context mContext;
    private RecyclerViewTV mRecyclerView;
    private MainUpView mainUpView1;
    private RecyclerViewBridge mRecyclerViewBridge;
    private View oldView;
    private SettingRecyclerViewPresenter mRecyclerViewPresenter;
    private GeneralAdapter mGeneralAdapter;
    //默认选中的位置
    private int mSavePos = 0;
    private String str[] = new String[]{"常规", "网络", "网络测试", "视频", "音频", "摄像头调节", "通话设置", "关于"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        //        customerOnOff = (CustomerOnOff)findViewById(R.id.custom_onoff);
        //
        //        customerOnOff.setOnClickListener(new CustomerOnOff.onClickListener() {
        //            @Override
        //            public void onLeftRowClick() {
        //                Toast.makeText(SettingActivity.this, "点击关箭头", Toast.LENGTH_SHORT).show();
        //                customerOnOff.stateChange("关");
        //            }
        //
        //            @Override
        //            public void onRightRowClick() {
        //                Toast.makeText(SettingActivity.this, "onRightRowClick点击开箭头", Toast.LENGTH_SHORT).show();
        //                customerOnOff.stateChange("开");
        //            }
        //        });
        mContext = SettingActivity.this;
        mRecyclerView = (RecyclerViewTV) findViewById(R.id.recyclerView);
        mainUpView1 = (MainUpView) findViewById(R.id.mainUpView1);
        mainUpView1.setEffectBridge(new RecyclerViewBridge());
        // 注意这里，需要使用 RecyclerViewBridge 的移动边框 Bridge.
        mRecyclerViewBridge = (RecyclerViewBridge) mainUpView1.getEffectBridge();
        mRecyclerViewBridge.setUpRectResource(R.drawable.blue_frame);
        float density = getResources().getDisplayMetrics().density;
        RectF receF = new RectF(getDimension(R.dimen.w_5) * density, getDimension(R.dimen.h_5) * density,
                getDimension(R.dimen.w_5) * density, getDimension(R.dimen.h_5) * density);
        mRecyclerViewBridge.setDrawUpRectPadding(receF);
        recyclerViewLinerLayout(RecyclerView.VERTICAL);

        //
        mRecyclerView.setOnItemListener(this);
        // item 单击事件处理.
        mRecyclerView.setOnItemClickListener(new RecyclerViewTV.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerViewTV parent, View itemView, int position) {
                Toast.makeText(mContext, "点击位置" + position, Toast.LENGTH_SHORT).show();
                enterSelectedActivty(position);
            }
        });

    }


    public void enterSelectedActivty(int postion) {
        Intent intent = new Intent();
        switch (postion) {
            case 0:
                intent.setClass(mContext, GeneralTActivity.class);
                break;
            case 1:
                intent.setClass(mContext, NetWorkActivity.class);
                break;
            case 2:
                intent.setClass(mContext, NetTestActivity.class);
                break;
            case 3:
                intent.setClass(mContext, VideoSetActivity.class);
                break;
            case 4:
                intent.setClass(mContext, VoiceSetActivity.class);
                break;
            case 5:
                intent.setClass(mContext, TVCameraActivity.class);
                break;
            case 6:
                intent.setClass(mContext, AboutActivity.class);
                break;
            case 7:
                intent.setClass(mContext, AboutActivity.class);
                break;
        }
        startActivity(intent);
    }


    public float getDimension(int id) {
        return getResources().getDimension(id);
    }

    /**
     * 测试LinerLayout.
     */
    private void recyclerViewLinerLayout(int orientation) {
        LinearLayoutManagerTV layoutManager = new LinearLayoutManagerTV(this);
        layoutManager.setOrientation(orientation);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setFocusable(false);
        mRecyclerViewPresenter = new SettingRecyclerViewPresenter(str);
        mGeneralAdapter = new GeneralAdapter(mRecyclerViewPresenter);
        mRecyclerView.setAdapter(mGeneralAdapter);
        mRecyclerView.setDefaultSelect(mSavePos);
    }

    @Override
    public void onItemPreSelected(RecyclerViewTV parent, View itemView, int position) {
        mRecyclerViewBridge.setUnFocusView(oldView);
    }

    @Override
    public void onItemSelected(RecyclerViewTV parent, View itemView, int position) {
        mRecyclerViewBridge.setFocusView(itemView, 1.0f);
        oldView = itemView;
    }

    @Override
    public void onReviseFocusFollow(RecyclerViewTV parent, View itemView, int position) {
        mRecyclerViewBridge.setFocusView(itemView, 1.0f);
        oldView = itemView;
    }
}
